package org.adobe.utils {

	/**字符串常用方法*/
	public class StringUtil {
		
		/**字母比较字符是否相等*/
		public static function equalsIgnoreCase(s1:String, s2:String, caseBool:Boolean = false):Boolean {
			if (caseBool){
				return (s1 == s2);
			} else {
				return (s1.toUpperCase() == s2.toUpperCase());
			}
		}

		/**去左右空格*/ //trimArrayElements(str,"|")  参数隔开部分空白去掉
		public static function trim(input:String):String {
			return StringUtil.ltrim(StringUtil.rtrim(input));
		}

		/**去左空格*/
		public static function ltrim(input:String):String {
			var size:Number = input.length;
			for (var i:Number = 0; i < size; i++){
				if (input.charCodeAt(i) > 32){ //索引位置处的字符的数值 Unicode字符代码,32空格
					return input.substring(i); //charAt()  charCodeAt()
				}
			}
			return "";
		}

		/**去右空格*/
		public static function rtrim(input:String):String {
			var size:Number = input.length;
			for (var i:Number = size; i > 0; i--){
				if (input.charCodeAt(i - 1) > 32){
					return input.substring(0, i);
				}
			}
			return "";
		}

		/**
		 * 是否为该字符的前缀字符串
		 */
		public static function beginsWith(input:String, prefix:String):Boolean {
			return (prefix == input.substring(0, prefix.length));
		}

		/**
		 * 是否为该字符的后缀字符串
		 */
		public static function endsWith(input:String, suffix:String):Boolean {
			return (suffix == input.substring(input.length - suffix.length));
		}

		/**
		 * 去除指定字符串
		 */
		public static function remove(input:String, remove:String):String {
			return StringUtil.replace(input, remove, "");
		}

		/**
		 * 字符串替换
		 * @param input
		 * @param replace      被替换内容
		 * @param replaceWith  替换内容
		 */
		public static function replace(input:String, replace:String, replaceWith:String):String {
			//return input.replace(replace,replaceWith);
			return input.split(replace).join(replaceWith);
		}

		/**字符串是含值*/
		public static function stringHasValue(s:String):Boolean {
			//todo: this needs a unit test
			return (s != null && s.length > 0);
		}

		/**是否为空白*/
		public static function isWhitespace(char:String):Boolean {
			switch (char){
				case " ":
				case "\t":
				case "\r":
				case "\n":
				case "\f":
					return true;
				default:
					return false;
			}
		}

		/**URL地址*/
		public static function isURL(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char).toLowerCase();
			var reg:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**是否注册字符*/
		public static function hasAccountChar(char:String, len:uint = 15):Boolean {
			if (char == null){
				return false;
			}
			if (len < 10){
				len = 15;
			}
			char = trim(char);
			var reg:RegExp = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9_-]{0," + len + "}$", "");
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**是否为邮箱*/
		public static function isEmail(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**双字节*/
		public static function isDoubleChar(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /^[^\x00-\xff]+$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**是否含中文字符*/
		public static function hasChineseChar(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /[^\x00-\xff]/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**是否中文*/
		public static function isChinese(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /^[\u0391-\uFFE5]+$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**英文*/
		public static function isEnglish(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /^[A-Za-z]+$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**是否是数值字符串*/
		public static function isNumber(char:String):Boolean {
			if (char == null){
				return false
			}
			return !isNaN(Number(char)) // NaN--非数字
		}

		/**是否为Double型数据*/
		public static function isDouble(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /^[-\+]?\d+(\.\d+)?$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		/**Integer*/
		public static function isInteger(char:String):Boolean {
			if (char == null){
				return false;
			}
			char = trim(char);
			var reg:RegExp = /^[-\+]?\d+$/;
			var result:Object = reg.exec(char);
			if (result == null){
				return false;
			}
			return true;
		}

		function StringUtil(){
			throw new Error("StringUtil class is static container only");
		}
	}
}